package com.example.dormitorysystem.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.dormitorysystem.common.aop.OperationLogAnnotation;
import com.example.dormitorysystem.entity.Auth;
import com.example.dormitorysystem.entity.Logs;
import com.example.dormitorysystem.exception.CmsException;
import com.example.dormitorysystem.exception.ErrorCode;
import com.example.dormitorysystem.mapper.AuthMapper;
import com.example.dormitorysystem.mapper.LogsMapper;
import com.example.dormitorysystem.service.AuthService;
import com.example.dormitorysystem.utils.IPUtils;
import com.example.dormitorysystem.utils.Response;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 吉吉王国组
 * @since 2022-11-13
 */
@RestController
@RequestMapping("/auth")
public class AuthController {

    @Resource
    private AuthService authService;

    @Resource
    private AuthMapper authMapper;

    @Resource
    private LogsMapper logsMapper;

    @PostMapping("/register")
    public Response register(HttpServletRequest request, HttpServletResponse response) {
        Auth auth = new Auth();
        auth.setUsername("2201210159");
        auth.setPassword("123456");
        auth.setType(1);
        auth.setAddTime(new Date());
        auth.setUid(2);
        auth.setRemarks("无");
        QueryWrapper<Auth> authMapperQueryWrapper = new QueryWrapper<>();
        authMapperQueryWrapper.eq("username", auth.getUsername());
        if(authMapper.selectOne(authMapperQueryWrapper) != null) {
            throw new CmsException(400, "账号已存在，请直接登录！");
        }
        authMapper.insert(auth);
        return Response.success();
    }



    @OperationLogAnnotation(operModul = "用户模块", operType = "UserLogin", operDesc = "用户登录")
    @PostMapping("/login")
    public Response login(@RequestBody Map<String, Object> loginInput, HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> res = authService.login(loginInput, request, response);
        return Response.success().data(res);
    }


    @PostMapping("/refreshtoken")
    public Response refreshToken(@RequestBody Map<String, Object> loginInput, HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> res = authService.refreshToken((String) loginInput.get("token"),
                (String) loginInput.get("refresh_token"), request, response);
        return Response.success().data("data", res);
    }

    @PostMapping("/logout")
    @OperationLogAnnotation(operModul = "用户模块", operType = "UserLogout", operDesc = "用户退出")
    public Response logout(HttpServletRequest request) {
       authService.logout(request);
        return Response.success();
    }
}

